package com.aiyan.edu.service.learning.service;

import com.aiyan.edu.framework.domain.course.CoursePlanMediaPub;
import com.aiyan.edu.framework.domain.learning.LearningCourse;
import com.aiyan.edu.framework.domain.learning.respones.GetMediaResult;
import com.aiyan.edu.framework.domain.learning.respones.LearningCode;
import com.aiyan.edu.framework.domain.task.Task;
import com.aiyan.edu.framework.domain.task.TaskHis;
import com.aiyan.edu.framework.exception.ExceptionThrow;
import com.aiyan.edu.framework.model.response.CommonCode;
import com.aiyan.edu.framework.model.response.CommonResponseResult;
import com.aiyan.edu.framework.model.response.ResponseResult;
import com.aiyan.edu.service.learning.client.CourseEsSearchClient;
import com.aiyan.edu.service.learning.dao.LearningCourseRepository;
import com.aiyan.edu.service.learning.dao.TaskHisRepository;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Optional;

/**
 * @author: zqzhou
 * @create: 2019/11/20 23:32
 **/
@Service
public class CourseLearningService {

    @Autowired
    CourseEsSearchClient courseEsSearchClient;

    @Autowired
    LearningCourseRepository learningCourseRepository;

    @Autowired
    TaskHisRepository taskHisRepository;

    public GetMediaResult findMedia(String courseId, String coursePlanId) {

        // TODO: 2019/11/20 校验学生是否有该课程学习权限

        CommonResponseResult<CoursePlanMediaPub> courseMedia = this.courseEsSearchClient.findCourseMedia(coursePlanId);
        if (!courseMedia.isSuccess() || courseMedia.getResult() == null) {
            ExceptionThrow.out(LearningCode.LEARNING_GETMEDIA_ERROR);
        }
        return new GetMediaResult(CommonCode.SUCCESS, courseMedia.getResult().getMediaUrl());
    }

    /**
     * 添加选课
     *
     * @param userId
     * @param courseId
     * @param valid
     * @param startTime
     * @param endTime
     * @param task
     */
    @Transactional
    public ResponseResult choseCourse(String userId, String courseId, String valid, Date startTime, Date endTime, Task task) {
        if (StringUtils.isEmpty(courseId)) {
            ExceptionThrow.out(CommonCode.INVALID_PARAM);
        }
        if (StringUtils.isEmpty(userId)) {
            ExceptionThrow.out(CommonCode.INVALID_PARAM);
        }
        if (task == null || StringUtils.isEmpty(task.getId())) {
            ExceptionThrow.out(CommonCode.INVALID_PARAM);
        }

        try {
            LearningCourse learningCourse = this.learningCourseRepository.findByCourseIdAndUserId(userId, courseId);

            if (null != learningCourse) {
                //更新选课记录
                learningCourse.setStartTime(startTime);
                learningCourse.setEndTime(endTime);
                learningCourse.setStatus("501001");
                this.learningCourseRepository.save(learningCourse);
            } else {
                //添加选课记录
                learningCourse = new LearningCourse();
                learningCourse.setUserId(userId);
                learningCourse.setCourseId(courseId);
                learningCourse.setStartTime(startTime);
                learningCourse.setEndTime(endTime);
                learningCourse.setStatus("501001");
                this.learningCourseRepository.save(learningCourse);
            }

            // 选课历史，如果不存在则添加，存在则忽略
            Optional<TaskHis> taskHisOptional = this.taskHisRepository.findById(task.getId());
            if (!taskHisOptional.isPresent()) {
                TaskHis taskHis = new TaskHis();
                BeanUtils.copyProperties(task, taskHis);
                this.taskHisRepository.save(taskHis);
            }

            return ResponseResult.SUCCESS();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
